package tuan.filter;

import org.w3c.dom.Node;

public class TextLengthFiltter extends NodeFilter {

	int length;
	String op;

	public TextLengthFiltter(int length, String op) throws Exception {
		if (op == null) {
			throw new Exception("null arguments");
		}
		if (length < 0) {
			throw new Exception("invalid arguments");
		}
		this.length = length;
		this.op = op;
	}

	protected int getTextLen(Node node) {
		return node.getTextContent().length();
	}

	@Override
	protected boolean isValid(Node node) {
		int textLen = getTextLen(node);
		if (op.equals("=")) {
			return textLen == length;
		}
		if (op.equals(">")) {
			return textLen > length;
		}
		if (op.equals(">=")) {
			return textLen >= length;
		}
		if (op.equals("<")) {
			return textLen < length;
		}
		if (op.equals("<=")) {
			return textLen <= length;
		}
		if (op.equals("!=")) {
			return textLen != length;
		}
		return false;
	}

}
